package com.mlink.web;

import com.mlink.entity.RecordEntity;
import com.mlink.entity.RecordInfoEntity;
import com.mlink.mapper.RecordMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
public class RecordController {
	
	@Autowired
	private RecordMapper recordMapper;



    /*
    * 获取所有日志
    *
    * */
	@RequestMapping(value = "/getRecords",method = RequestMethod.GET)
//
    public List<RecordEntity> getRecords(@RequestParam(value = "start", required = true) int start,  //  required = false  int 要换成Integer
                                         @RequestParam(value = "num", required = true) int num,
                                         @RequestParam(value = "userID", required = false) Integer userID,
                                         @RequestParam(value = "projectID", required = false) Integer projectID,
                                         @RequestParam(value = "date", required = false) String date) {

		List<RecordEntity> users=recordMapper.getAll(start,num, userID, projectID,  date);


		return users;
	}


   /*
    * 获取日志列表信息
    *
    * */
    @RequestMapping(value = "/getRecordList",method = RequestMethod.GET)
    public List<RecordInfoEntity> getRecordList(@RequestParam(value = "start", required = true) int start,  //  required = false  int 要换成Integer
                                         @RequestParam(value = "num", required = true) int num,
                                         @RequestParam(value = "userID", required = false) Integer userID,
                                         @RequestParam(value = "projectID", required = false) Integer projectID,
                                         @RequestParam(value = "date", required = false) String date) {

        List<RecordInfoEntity> users=recordMapper.getRecordList(start,num, userID, projectID,  date);


        return users;
    }

    /*
  * 获取所有用户
  *
  * */
    @RequestMapping("/getOneRecord")

    public RecordEntity getUsers(int id) {

        RecordEntity users=recordMapper.getOne(id);
        return users;
    }
    /*
    * 添加日志
    *
    * */
    @RequestMapping(value = "/addRecord", method = RequestMethod.POST)
    public ModelMap save(@RequestBody RecordEntity record   ) {

        Date d = new Date();
        System.out.println(d);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateNowStr = sdf.format(d);
        record.setOptDate(dateNowStr);


        ModelMap  mm = new ModelMap();
        List<RecordEntity> result =  recordMapper.isEixt(record.getUserID(), record.getProjectID(),record.getDate());

        if(result.size() > 0) {
//            recordMapper.updateInfo(record);
            mm.put("result", 2);  //更新 成功

        } else {

            recordMapper.insert(record);
                mm.put("result", 1);  // 成功
                mm.put("id", record.getId());  // 成功

        }
        return mm;

    }
//
    /*
    *更新用户
    *
    * */
    @RequestMapping(value="/updateRecord",method = RequestMethod.POST)
    public ModelMap update(@RequestBody RecordEntity user) {
        ModelMap  mm = new ModelMap();
    	int result =  recordMapper.update(user);
        if (result == 1) {
            mm.put("result", 1);

        } else {
            mm.put("result", 0);
        }
        return  mm;
    }
//
    @RequestMapping(value="/deleteRecord" )
    public ModelMap delete(int id) {
        ModelMap  mm = new ModelMap();
         int result = recordMapper.delete(id);
        if (result == 1) {
            mm.put("result", 1);

        } else {
            mm.put("result", 0);
        }
        return mm;
    }


}